//
//  CollectionListViewController.m
//  QuicklyPlay
//
//  Created by 你的温柔是我唯一沉溺 on 15/10/31.
//  Copyright © 2015年 OPteam. All rights reserved.
//
#define KScreen_Width [UIScreen mainScreen].bounds.size.width
#define KScreen_Height [UIScreen mainScreen].bounds.size.height

#import "CollectionListViewController.h"
#import "SortListSecondCell.h"
#import "DataManager.h"
#import "MPPlayViewController.h"
#import "NetWorkViewController.h"

const CGFloat BackGroupHeight = 200;
const CGFloat HeadImageHeight= 80;

@interface CollectionListViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
{
    UITableView *collectionTableView;
    UIImageView *imageBG;
    UIView *BGView;
    UIImageView *headImageView;
    UILabel *nameLabel;
    UILabel *titleLabel;
   
}

@property (strong,nonatomic) NSMutableArray *array ;

@end

@implementation CollectionListViewController


-(NSMutableArray *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
//    NSKeyedUnarchiver * arc = [NSKeyedUnarchiver unarchiveObjectWithFile:SelectedSortFile] ;
//    
//    NSDictionary *dict = (NSDictionary *)arc;
//    for (VideosModel *model in dict) {
//        VideosModel *aModel = [[VideosModel alloc] init];
//        aModel = model;
//        NSLog(@"%@",model.title);
//        [self.array addObject: aModel];
//        
//    }

    [self getArray];
}

- (void)getArray{
//    [self.array removeAllObjects];
    self.array = [[DataManager sharedInstance] getData];
//    [self.array  removeAllObjects];
    //    [self.array addObject:array];
//    self.array = [array copy];
    //    for (NSDictionary *dic in array) {
    //        VideosModel *model = [[VideosModel alloc] init];
    //        [model setValuesForKeysWithDictionary:dic];
    //        [self.array addObject:model];
    //    }
}




- (IBAction)barItemBack:(UIBarButtonItem *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];

}



- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets=NO;//自动布局
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    [self setupUI];
    
    [[GestureRecognizerTool creatScreenEdgePanGestureRecognizerLeftWithViewController:self] addTarget:self action:@selector(barItemBack:)];
   
}


-(void)setupUI
{
    collectionTableView=[[UITableView alloc]init];
    collectionTableView.delegate=self;
    collectionTableView.dataSource=self;
    [collectionTableView registerNib:[UINib nibWithNibName:@"SortListSecondCell" bundle:nil] forCellReuseIdentifier:@"cell"] ;
    collectionTableView.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width,  [UIScreen mainScreen].bounds.size.height);
    collectionTableView.contentInset=UIEdgeInsetsMake(BackGroupHeight, 0, 0, 0);
    collectionTableView.separatorStyle = UITableViewCellSeparatorStyleNone ;
    [self.view addSubview:collectionTableView];
    
    //
    imageBG=[[UIImageView alloc]init];
    imageBG.frame=CGRectMake(0, -BackGroupHeight, KScreen_Width, BackGroupHeight);
    imageBG.image=[UIImage imageNamed:@"IMG_1292.JPG"];
    [collectionTableView addSubview:imageBG];
    
    //
    BGView=[[UIView alloc]init];
    BGView.backgroundColor=[UIColor clearColor];
    BGView.frame=CGRectMake(0, -BackGroupHeight, KScreen_Width, BackGroupHeight);
    [collectionTableView addSubview:BGView];
    
    //设置图像啊
    headImageView=[[UIImageView alloc]init];
    [headImageView sd_setImageWithURL:[NSURL URLWithString:self.model.imageHeader]];
    
    
    
    headImageView.frame=CGRectMake((KScreen_Width-HeadImageHeight)/2, 50, HeadImageHeight, HeadImageHeight);
    
    headImageView.layer.cornerRadius = HeadImageHeight / 2 ;
    headImageView.layer.masksToBounds = YES;
    [BGView addSubview:headImageView];
    
    //设置用户名啊
    nameLabel=[[UILabel alloc]init];
    nameLabel.text=self.model.name;
    nameLabel.textAlignment=NSTextAlignmentCenter;
    nameLabel.frame=CGRectMake((KScreen_Width-HeadImageHeight)/2, CGRectGetMaxY(headImageView.frame), HeadImageHeight, 20);
    //nameLabel.backgroundColor=[UIColor whiteColor];
    [BGView addSubview:nameLabel];
    
    //设置标题啊
    titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.textColor=[UIColor whiteColor];
    titleLabel.text=@"title";
    titleLabel.textAlignment=NSTextAlignmentCenter;
    self.navigationItem.titleView=titleLabel;
    titleLabel.alpha=0;
    
}







#pragma mark - tableViewDelegate&DataSource

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
   SortListSecondCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    //VideosModel *model = [DataManager sharedInstance].dataArray[indexPath.row];
    
   VideosModel * model = self.array[indexPath.row];
    NSLog(@"%@",model.m3u8_url);
    
    [cell setCollectionModel:model];
    

    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
     VideosModel * model = self.array[indexPath.row];
    
    if ([model.m3u8_url isEqualToString:@""]) {
        
        NetWorkViewController *web = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"netWorkVC"];
       
        [self presentViewController:web animated:YES completion:nil];
        
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:model.web_url]];
        [web.webView loadRequest:request];
    }
    if ([NetworkHelper judgeNetworkStatus]) {
        MPPlayViewController *playController = [[MPPlayViewController alloc] initWithContentURL:[NSURL URLWithString:model.m3u8_url]];
        
        [self presentMoviePlayerViewControllerAnimated:playController];
    }
    
}


#pragma mark 删除cell的方法
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES ;
}



-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete ;
}



-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    [[DataManager sharedInstance]deleteDataWithIndex:indexPath.row];
   
    [tableView reloadData];
     [self getArray];
}





-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat yOffset  = scrollView.contentOffset.y;
    CGFloat xOffset = (yOffset + BackGroupHeight)/2;
    if (yOffset < -BackGroupHeight) {
        CGRect rect = imageBG.frame;
        rect.origin.y = yOffset;
        rect.size.height =  -yOffset ;
        rect.origin.x = xOffset;
        rect.size.width = KScreen_Width + fabs(xOffset)*2;
        imageBG.frame = rect;
        
    }
    
    CGFloat alpha = (yOffset+BackGroupHeight)/BackGroupHeight;
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor orangeColor]colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];
    titleLabel.alpha=alpha;
    alpha=fabs(alpha);
    alpha=fabs(1-alpha);
    alpha=alpha<0.2? 0:alpha-0.2;
    BGView.alpha=alpha;
    
}


- (UIImage *)imageWithColor:(UIColor *)color
{
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    // 开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 渲染上下文
    CGContextFillRect(context, rect);
    // 从上下文中获取图片
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    // 结束上下文
    UIGraphicsEndImageContext();
    return theImage;
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}


@end
